package com.hup.minicontroller.client.view.main;

import com.hup.minicontroller.client.model.LanBroadcastServer;
import com.hup.minicontroller.client.model.ScannedServerItem;
import com.hup.utils.swing.customView.ImageButton;
import lombok.Setter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author hugan
 * @date 2021/5/23
 */
public class ScannedServerItemForm extends JPanel implements ActionListener {
    private static final ImageIcon ICON_LAN = new ImageIcon(ScannedServerItemForm.class.getResource("/img/main/icon_lan.png"));
    private static final ImageIcon ICON_BLUETOOTH = new ImageIcon(ScannedServerItemForm.class.getResource("/img/main/icon_lan.png"));

    public JPanel root;
    public ImageButton imgIcon;
    public JLabel txtServerName;
    public JLabel txtAddress;
    public JButton btnConnect;

    @Setter
    private ConnectListener connectListener;
    private ScannedServerItem server;

    public ScannedServerItemForm() {
        $$$setupUI$$$();
        btnConnect.addActionListener(this);
    }

    private void createUIComponents() {
        root = this;
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        root.setLayout(new GridBagLayout());
        imgIcon = new ImageButton();
        imgIcon.setIcon(new ImageIcon(getClass().getResource("/img/main/icon_lan.png")));
        imgIcon.setUseSharedIcon(true);
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        root.add(imgIcon, gbc);
        txtServerName = new JLabel();
        txtServerName.setText("服务端名");
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        root.add(txtServerName, gbc);
        txtAddress = new JLabel();
        txtAddress.setText("ip");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 10, 0, 0);
        root.add(txtAddress, gbc);
        btnConnect = new JButton();
        btnConnect.setText("连接");
        gbc = new GridBagConstraints();
        gbc.gridx = 3;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        root.add(btnConnect, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() { return root; }

    public void setServer(ScannedServerItem item) {
        server = item;
        if (item.isLan()) {
            imgIcon.setIcon(ICON_LAN);
            LanBroadcastServer lanBroadcastServer = item.getLanBroadcastServer();
            txtServerName.setText(lanBroadcastServer.getServerName());
            txtAddress.setText(lanBroadcastServer.getAddress());
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (connectListener == null) return;
        connectListener.doConnect(server);
    }

    public interface ConnectListener {
        void doConnect(ScannedServerItem item);
    }

}
